package com.app.teaware.notice;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.teaware.R;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.basis.Constants;
import com.app.teaware.bean.BusiTypeEmum;
import com.app.teaware.bean.NewsDetailBean;
import com.app.teaware.bean.ProjectFileListBean;
import com.app.teaware.common.AdapterFileList;
import com.app.teaware.view.H5contentWebview;

import butterknife.BindView;
import my.ActivityTool;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

/**
 * 详情
 */
public class NoticeDetailActivity extends BasisActivity {

    public static void toDetail(Context context, String id) {
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        ActivityTool.skipActivity(context, NoticeDetailActivity.class, bundle);
//        Intent intent= new Intent(context,NoticeDetailActivity.class);
//        intent.putExtra("id",id);
//        context.startActivity(intent);
    }

//    @BindView(R.id.webview)
    H5contentWebview webview;
    @BindView(R.id.text_title)
    TextView text_title;
    @BindView(R.id.text_time)
    TextView text_time;
    @BindView(R.id.text_auther)
    TextView text_auther;
    @BindView(R.id.view_webview_zone)
    LinearLayout view_webview_zone;

    String id;
    NewsDetailBean mDetailBean;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.notice_detail_activity);

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        setTitle("通知公告");
        setTitleLeftButton(null);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        id = getIntent().getStringExtra("id");

        if (TextUtils.isEmpty(id)) {
            showToast("文章ID不存在");
            finish();
            return;
        }
        getDetail();
        getFiles();
    }


    private void getDetail() {
        MyParams params = new MyParams();
        showProgress();
        params.put("id", id);

        showLoading();

        MyHttpUtils.post(Constants.URL_NOTICE_DETAIL, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        mDetailBean = (NewsDetailBean) result;
                        setDetailView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        hideLoading();
                    }
                },
                0, NewsDetailBean.class);

    }

    void setDetailView() {
        if (mDetailBean == null) return;
        webview = new H5contentWebview(mContext);
        webview.initActivity(mContext);
        text_title.setText(mDetailBean.getTitle());
        text_time.setText("发布时间：" + mDetailBean.getCreateTime());
        text_auther.setText("作者：" + mDetailBean.getAuthor());
        webview.setContent(mDetailBean.getContent());
        view_webview_zone.addView(webview,0,new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)));
//        webview.setText(Html.fromHtml(mDetailBean.getContent(),new UrlImageGetter(webview,mContext),null));
    }

    private void getFiles() {
        MyParams params = new MyParams();
        showProgress();
        params.put("busiType", BusiTypeEmum.通知公告附件.getName());
        String[] ids = new String[1];
        ids[0] = id;
        params.putObj("busiIds", ids);
//        params.put("fileType", FileTypeEmum.Other);

        MyHttpUtils.post(Constants.URL_USER_ATTACHMENT_LIST, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        ProjectFileListBean fileListBean = (ProjectFileListBean) result;
                        recyclerView.setAdapter(new AdapterFileList(mContext, fileListBean));
                    }

                    @Override
                    public void onFinish(int httpWhat) {
//                        hideLoading();
                    }
                },
                0, ProjectFileListBean.class);

    }
}